﻿using DABLL;
using DADAL;
using WinFormium;
using WinFormium.Forms;

namespace DataAcquisition
{
    internal class ReportFormium : Formium
    {
        #region 属性

        /// <summary>
        /// 试压工程
        /// </summary>
        public ProjectModel? ProjectItemServices;

        /// <summary>
        /// 试压模板
        /// </summary>
        public TemplateModel? TemplateModelServices;

        /// <summary>
        /// 试压记录
        /// </summary>
        public TestRecordModel? TestRecordModels;

        /// <summary>
        /// 图片数据
        /// </summary>
        public string ImageData = string.Empty;

        /// <summary>
        /// 最终结果
        /// </summary>
        public bool FinalResult = false;

        #endregion

        #region 自定义方法

        /// <summary>
        /// 注册方法
        /// </summary>
        private void RegisterJavaScriptMessagHandler()
        {
            RegisterJavaScriptMessagHandler("CheckPermission", args =>
            {
                string? data = args;
                if (data is not null)
                {

                }
            });
            RegisterJavaScriptMessagHandler("FormState", args =>
            {
                string? data = args;
                if (data is not null)
                {
                    if (data == "close")
                    {
                        this.Close();
                    }
                }
            });
        }

        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="res"></param>
        private void UpdateInfo(string id, string res)
        {
            string js = string.Format("updateInfo('#{0}','{1}')", id, res);
            EvaluateJavaScript(js);

        }

        private void EvaluateJavaScript(string js)
        {
            if (js is not null)
            {
                try
                {
                    EvaluateJavaScriptAsync(js);
                }
                catch (Exception)
                {
                }
            }
        }
        #endregion

        #region 系统方法

        public ReportFormium()
        {
            ///不显示窗体过度动画
            EnableSplashScreen = false;
            Icon icon = new Icon("logo.ico");
            Icon = icon;
            Sizable = false;//禁止调整窗口大小
            Url = "http://dataqcquisition.app.local/Report.html";
            PageLoadEnd += IndexFormium_PageLoadEnd;
            RegisterJavaScriptMessagHandler();
        }
        protected override FormStyle ConfigureWindowStyle(WindowStyleBuilder builder)
        {
            var style = builder.UseSystemForm();
            style.TitleBar = false;
            style.DefaultAppTitle = "数据采集";
            style.Maximizable = true;
            style.WindowState = FormiumWindowState.Maximized;
            style.StartCentered = StartCenteredMode.CenterScreen;
            style.ColorMode = FormiumColorMode.Light;

            if (Screen.PrimaryScreen is not null)
            {
                style.MinimumSize = Screen.PrimaryScreen.WorkingArea.Size;
            }
            return style;
        }

        /// <summary>页面加载完成
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void IndexFormium_PageLoadEnd(object? sender, PageLoadEndEventArgs e)
        {
            if (ProjectItemServices is not null)
            {
                UpdateInfo("project_name", ProjectItemServices.Name);
                UpdateInfo("project_number", ProjectItemServices.Number);
                UpdateInfo("stake_mark", ProjectItemServices.StakeMark);
                UpdateInfo("pipe_specification", ProjectItemServices.PipeSpecification);
                UpdateInfo("design_pressure", ProjectItemServices.DesignPressure.ToString()+"MPa");
                UpdateInfo("pipe_length", ProjectItemServices.PipeLength.ToString() + "米");
                UpdateInfo("medium", ProjectItemServices.Medium);
            }

            if (TestRecordModels is not null)
            {
                Dictionary<string, Object> where = new Dictionary<string, Object>();
                where["number"] = TestRecordModels.Number;
                List<TestRecordModel> list = SqlSugarHelper.Inquireable<TestRecordModel>(where);
                if (list.Count > 0)
                {
                    TestRecordModel model = list[0];
                    string start_time = model.StartTime;
                    string end_time = model.EndTime;
                    UpdateInfo("date", string.Format("{0} 至 {1}", start_time, end_time));
                }
            }

            if (ImageData.Length > 0)
            {
                string js = string.Format("updateImage('{0}')", ImageData);
                EvaluateJavaScript(js);

            }

            if (TemplateModelServices is not null)
            {
                UpdateInfo("intensity_pressure", TemplateModelServices.IntensityStagePressure.ToString());
                UpdateInfo("intensity_time", TemplateModelServices.IntensityStageTime.ToString()+ TemplateModelServices.IntensityStageTimeUnit);
                UpdateInfo("intensity_difference", "0");

                UpdateInfo("rigor_pressure", TemplateModelServices.RigorStagePressure.ToString());
                UpdateInfo("rigor_time", TemplateModelServices.RigorStageTime.ToString() + TemplateModelServices.RigorStageTimeUnit);
                UpdateInfo("rigor_difference", "0");
            }

            if (TestRecordModels is not null)
            {
                UpdateInfo("result", TestRecordModels.Result == 1 ? "合格" : "不合格");
            }
        }
        #endregion
    }
}
